#-------------------------------------------------------------------------------
# Name:        模块1
# Purpose:
#
# Author:      Administrator
#
# Created:     18/08/2019
# Copyright:   (c) Administrator 2019
# Licence:     <your licence>
#-------------------------------------------------------------------------------
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import re
import json
import wx

driver =None

key_words_control=None
page_num_control=None
out_file_name_control=None
keywords = ""
page_num = 10
out_file_name = "show_result_out"
window = None

def compare_weixin_account():
    account_file =open("search_weixin_account_out.txt","r")
    filter_file = open("area.txt")
    filter_str = filter_file.read()
    account_list = json.loads(account_file.read())
    print(len(account_list))
    after_filter = [];
    for search_str in account_list:
        print(search_str)
        match_obj = re.search(search_str,filter_str)
        if  not match_obj:
            after_filter.append(search_str)
    fo = open("compare_weixin_account_out.txt","w")
    save_str = json.dumps(after_filter,ensure_ascii=False)
    fo.write(save_str)
    fo.close()
    filter_file.close()
    account_file.close()
    pass



def search_weixin_account():
    global page_num,keywords
    url='https://weixin.sogou.com'
    driver=webdriver.Chrome()
    driver.get(url)
    driver.implicitly_wait(20)
    query_input = driver.find_element_by_id("query")
    query_input.send_keys(keywords)
    current_window = driver.current_window_handle
    print(current_window)
    query_input.send_keys(Keys.ENTER)
    account_list = []
    page = page_num;#获取多少页
    while page:
        get_weixin_account(driver,account_list)
        try:
            next_button = driver.find_element_by_id("sogou_next")
        except:
            break
        print(page)
        print(next_button)
        next_button.click()
        page -= 1


    print(len(account_list))
    print(account_list)
    save_str = json.dumps(account_list,ensure_ascii=False);
    fo = open("search_weixin_account_out.txt",'w')
    fo.write(save_str)
    fo.close()
    pass

def get_weixin_account(driver,account_list):
    time.sleep(1)
    accounts = driver.find_elements_by_class_name("account")
    print(len(accounts))
    for e in accounts:
        account_list.append(e.text)
        #print(e.text)



def parse_weixin_account():
    account_list = []
    dict = {}

    compared_file = open("compare_weixin_account_out.txt")
    compared_str = compared_file.read()
    compared_account = json.loads(compared_str)
    print(compared_account)

    url = "http://data.xiguaji.com/Home#/Search"
    driver=webdriver.Chrome()
    driver.get(url)
    time.sleep(15)
    search_input = driver.find_element_by_name("key")
    #search_input 在循环之后页面已经刷新了 是不能找到该元素的了 所以需要重新获取
    for search_key in compared_account:
        search_input.clear()
        search_input.send_keys(search_key)
        search_input.send_keys(Keys.ENTER)
        time.sleep(5)
        search_input = driver.find_element_by_name("key") #重新获取输入框
        js_search_result = driver.find_elements_by_class_name("list-piece")
        if not js_search_result:
            continue
        target_text = js_search_result[0].text
        patterns = {
            "帐号":"([\s\S]*?)账号主体",
            "主体":"账号主体([\s\S]*?)同主体公众号",
            "西瓜指数":"西瓜指数[\s\t\n]+(\d+\.?\d+)",
            "预估活跃粉丝":"预估活跃粉丝[\s\t\n]+(\d+\.?\d+?)",
            "头条平均阅读":"头条平均阅读[\s\t\n]+(\d+\.?\d+?)",
            "头条平均点赞":"头条平均点赞[\s\t\n]+(\d+\.?\d+?)",
            "头条平均留言":"头条平均留言[\s\t\n]+(\d+\.?\d+?)",
            "周发文篇数":"周发文篇数[\s\t\n]+(\d+)",
            "所属行业":"所属行业：([\s\S]*?)[\s\t\n]+",
            "运营地区":"运营地区：([\s\S]*?)[\s\t\n]+",
            "注册时间":"注册时间：([\s\S]*?)[\s\t\n]+"
        }
        dict = {}
        for pattern_key in patterns:
            match_obj = re.search(patterns[pattern_key],target_text)
            if match_obj:
                dict[pattern_key] = match_obj.group(1)
        #匹配的上的这个帐号的才算是 有效数据
        search_obj = re.search(search_key,dict['帐号'])
        if search_obj:
            print(dict)
            account_list.append(dict)
    #把分析数据写入文件
    fo = open("parse_weixin_account_out.txt",'w')
    fo.write(json.dumps(account_list,ensure_ascii=False))
    fo.close()
    pass

def test():
    names = ["旁门左道PPT","大连沙河口优智外国语培训学校"]
    #match方法从头开始匹配
    pattern="预估活跃粉丝[\s\t\n]+(\d+\.?\d+)"
    fo = open("test.txt",encoding="UTF-8")
    str = fo.read()

    account_list = []
    dict = {}


    account_list.append(dict)
    print(dict)
    print(account_list)
    pass

#可视化方法展示结果
def show_result():
    global out_file_name
    fo = open("parse_weixin_account_out.txt","r")
    account_list = fo.read()
    #print(account_list)

    account_list = json.loads(account_list)
    fo.close()
    keys = [
        "帐号",
		"主体",
		"西瓜指数",
		"预估活跃粉丝",
		"头条平均阅读",
		"头条平均点赞",
		"头条平均留言",
		"周发文篇数",
		"所属行业",
		"运营地区",
		"注册时间"
    ];
    table = ""
    for account in account_list:
        str = "<tr>"
        for key in keys:
            if key in account:
                str += "<td>"
                str += account[key]
                str += "</td>"
            else:
                str += "<td>--</td>"
        str += "</tr>"
        table += str
        pass
    pass
    #读取模版 替换其中的数据部分
    #print(table)
    result_template_file = open("result_template.txt","r",encoding="UTF-8")
    template = result_template_file.read()
    result = template.replace("####",table)
    result_template_file.close()
    fo = open(out_file_name +".html","w")
    fo.write(result)
    fo.close()



def init():
    global key_words_control,page_num_control,out_file_name_control,window
    app = wx.App()
    window = wx.Frame(None, title = "公众号采集系统", size = (400,300))
    panel = wx.Panel(window)
    key_words_label = wx.StaticText(panel, label = "搜素关键词", pos = (50,50))
    page_num_label = wx.StaticText(panel, label = "最大搜索多少页", pos = (50,100))
    out_file_name_label = wx.StaticText(panel,label="输出文件名",pos=(50,150))
    key_words_control = wx.TextCtrl(panel,pos=(150,50))
    page_num_control = wx.TextCtrl(panel,pos=(150,100))
    out_file_name_control = wx.TextCtrl(panel,pos=(150,150))
    button = wx.Button(panel,pos=(150,200),label="确定采集")
    button.Bind(wx.EVT_BUTTON,button_onclick)
    window.Show(True)
    app.MainLoop()
    pass

#操作都集中到这里了
def button_onclick(event):
    global window,keywords,page_num,out_file_name
    keywords = key_words_control.GetValue()
    page_num_tmp = page_num_control.GetValue()
    if page_num_tmp:
        page_num = int(page_num_tmp)
    out_file_name_tmp = out_file_name_control.GetValue()
    if len(out_file_name_tmp):
        out_file_name = out_file_name_tmp
    if not len(keywords):
        wx.MessageBox("请输入关键词", "提示" ,wx.OK | wx.ICON_INFORMATION)
        pass
    #生成临时文件保存待比较的公众号名称
    search_weixin_account()
    #输出已经过滤的公众号名称 文件为临时文件
    compare_weixin_account()
    #输出经过整理好数据的公众号文件 json 此文件可追加 不是临时文件
    parse_weixin_account()
    #test()
    #用html的形式展示数据 表格
    show_result()
    wx.MessageBox("采集完毕", "提示" ,wx.OK | wx.ICON_INFORMATION)
    pass

#操作可以选择 各个操作是相互独立的，只管各自的输入和输出 默认全部操作一遍
def main():
    print("init")
    init()
    pass

if __name__ == '__main__':
    main()
